<?php
namespace app\cms\controller;

use think\Db;
use think\Exception;

class Present
{
    /*
     * 商品同步 每天执行一次
     * 2017/8/8 蒋佳林 ADD
     *
     * */
    public function run()
    {
        $present = DB::table('homegarden.present')->alias('activity')
            ->join('homegarden_cms.product pro', 'pro.activity_id = activity.active_id')
            ->join('homegarden_cms.product pre', 'pre.`code` = activity.product_id', 'LEFT')
            ->join('homegarden.product product', 'product.product_id = activity.product_id', 'LEFT')
            ->where('pro.is_used', 1)
            ->where('pre.id', 'EXP', 'IS NULL')
            ->field('
                product.id,
                product.product_id `code`,
                product.product_name `name`,
                product.product_briefDescription brief_description,
                product.product_number number,
                product.product_unit unit,
                product.product_class1 category_class1,
                product.product_class2 category_class2,
                product.product_class3 category_class3,
                product.product_headImage thumb_image,
                product.real_weight,
                1 is_gift
            ')->select();
        if ($present) {
            DB::startTrans();
            try {
                DB::table('homegarden_cms.product')->insertAll($present);
            } catch (Exception $e) {
                DB::rollback();
                return $e->getMessage();
            }
            DB::commit();
        }
    }
}
